package com.yunche.yunchebusiness.mvp.presenter

import android.annotation.SuppressLint
import com.yunche.yunchebusiness.base.mvp.BasePresenter
import com.yunche.yunchebusiness.mvp.contract.SeriesContract
import com.yunche.yunchebusiness.mvp.model.SeriesModel
import com.yunche.yunchebusiness.network.exception.ErrorStatus
import com.yunche.yunchebusiness.network.exception.ExceptionHandle
import okhttp3.RequestBody

/**
 * Create by Crcker on 2018/12/21 15:12
 * Email: Crcker@163.com
 */

@SuppressLint("CheckResult")

class SeriesPresenter : BasePresenter<SeriesContract.View>(), SeriesContract.Presenter {

    private val model: SeriesModel by lazy {
        SeriesModel()
    }

    override fun getSeries(requestBody: RequestBody) {
        checkViewAttached()
        mRootView?.showLoading()
        var disposable = model.getSeries(requestBody).subscribe({
            mRootView?.apply {
                hideLoading()
                if (it.resultCode == ErrorStatus.SUCCESS) {
                    getSeriesSuccess(it.datas!!)
                } else {
                    showError(it.message!!)
                }
            }
        }, {
            mRootView?.hideLoading()
            mRootView?.showError(ExceptionHandle.handleException(it))
        })

        addSubscription(disposable)
    }

    override fun getModel(requestBody: RequestBody) {
        checkViewAttached()
        mRootView?.showLoading()
        var disposable =  model.getModel(requestBody).subscribe({
            mRootView?.apply {
                hideLoading()
                if (it.resultCode == ErrorStatus.SUCCESS) {
                    getModelSuccess(it.datas!!)
                } else {
                    showError(it.message!!)
                }
            }
        }, {
            mRootView?.hideLoading()
            mRootView?.showError(ExceptionHandle.handleException(it))
        })

        addSubscription(disposable)

    }
}